package cn.ecut.dao;

import cn.ecut.exception.DaoException;
import cn.ecut.model.Comment;
import cn.ecut.model.News;
import cn.ecut.util.JDBCHelper;
import cn.ecut.util.TableNames;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDao {
    public News OpenNews(String id){
        String Sql="select username,title,context,date,zan from"+ TableNames.NEWS_TABLE_NAME+"where id= ?";
        ResultSet resultSet= JDBCHelper.query(Sql,id);
        List<News> newsList=warp(resultSet);
        if(newsList.size()>0)
            return newsList.get(0);
        return null;

    }

    public boolean AddNews(News news){
        String sql="insert into"+TableNames.NEWS_TABLE_NAME+"(id,userid,username,sort,title,subtitle,context,date) values (?,?,?,?,?,?,?,?)";
        return JDBCHelper.execute(sql,news.getId(),news.getUserid(),news.getUsername(),news.getSort(),news.getTitle(),news.getSubtitle(),news.getContext(),news.getDate());
    }

    public boolean AddLikes(String id){
        String sql="Update"+TableNames.NEWS_TABLE_NAME+"set zan=zan+1 where id = ?";
        return JDBCHelper.execute(sql,id);
    }

    public List<News> NewsListBySort(String text) {
        ResultSet resultSet=null;
        if(text.equals("0")){
            String sql = "select username,title,subtitle,date,zan from" + TableNames.NEWS_TABLE_NAME ;
            resultSet = JDBCHelper.query(sql);
        }
        else {
            String sql = "select username,title,subtitle,date from" + TableNames.NEWS_TABLE_NAME + "where sort= ?";
            resultSet = JDBCHelper.query(sql, text);
        }
        List<News> newsList=warp2(resultSet);
        return newsList;
    }

    public List<News> NewsListByTitle(String text) {
        String sql="select username,title,subtitle,date,zan from"+TableNames.NEWS_TABLE_NAME+"where title= ?";
        ResultSet resultSet=JDBCHelper.query(sql,text);
        List<News> newsList=warp2(resultSet);
        return newsList;
    }

    private List <News> warp(ResultSet resultSet) throws DaoException {
        List<News> list=new ArrayList<>();
        try {
            while(resultSet.next()){
                News news1=new News();
                news1.setUsername(resultSet.getString("username"));
                news1.setTitle(resultSet.getString("title"));
                news1.setContext(resultSet.getString("context"));
                news1.setDate(resultSet.getDate("date"));
                news1.setZan(resultSet.getInt("zan"));
                list.add(news1);
            }
        } catch (SQLException e) {
            throw new DaoException("数据封装出错。");
        }
        return list;
    }
    private List <News> warp2(ResultSet resultSet) throws DaoException {
        List<News> list=new ArrayList<>();
        try {
            while(resultSet.next()){
                News news1=new News();
                news1.setUsername(resultSet.getString("username"));
                news1.setTitle(resultSet.getString("title"));
                news1.setSubtitle(resultSet.getString("subtitle"));
                news1.setDate(resultSet.getDate("date"));
                news1.setZan(resultSet.getInt("zan"));
                list.add(news1);
            }
        } catch (SQLException e) {
            throw new DaoException("数据封装出错。");
        }
        return list;
    }
}

